//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
var http = require('../../utils/server.js')
Page({
  data: {
    toView: 'to0',
    showAuthor: false,
    showOfficial:true,
    productList: [],
    menuData: [],
    footer: {
      index: 0
    },
    si: 0,
    selectMenu: {
      current: -1,
      selectedCurrent: -1,
      selectedChild: [],
      selectedChildName: [],
      isShow: false,
    }
  },


  onLoad: function(option) {
    console.log(new Date().getTime() + ' 来了老弟') 
    var that = this;
    const scene = decodeURIComponent(option.scene)
    var admin = scene || null
    that.setData({
      admin: admin
    })
    // var ui = wx.getStorageSync("userInfo");
    // that.setData({
    //   userInfo: ui
    // })
    //用户信息
    app.getUserInfo(function (userInfo) {
      if (userInfo != null) {
        that.setData({
          userInfo: userInfo,
          showAuthor: false,
          showOfficial: userInfo.gzhOpenid?false:true
        })

      } else {
        that.setData({
          showAuthor: true
        })
      }
    }, admin)
  },
  onShow: function() {
    var that = this;
    that.getData(that.data.userInfo)
  },

  onParentEvent: function(e) {
    app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, this.getData, this.data.admin)
  },

  getData: function(userInfo) {
    var that = this
    //散客 
    if (userInfo.gangId == '') {
      wx.redirectTo({
        url: '/pages/order/myorder/myorder'
      })
      return;
    }
    //商品列表
    http.post({
      url: "product/getProductList",
      type: ''
    }, function(data) {
      console.log('商品列表', data)
      that.setData({
        productList: data
      })
    })
    //筛选条件
    http.post({
      url: "product/getFilterData"
    }, function(data) {
      console.log('筛选条件', data)
      that.setData({
        menuData: data
      })
    })

    http.post({
      url: "index/getRedPointNum"
    }, function(data) {
      console.log('红点', data)
      that.setData({
        red_packet: data.red_packet
      })
    })

    //公告
    http.post({
      url: "index/getNotice"
    }, function(data) {
      console.log('公告列表', data)

      var readNotice = wx.getStorageSync("readNotice");
      for (var i in data) {
        for (var r in readNotice) {
          if (data[i].id == readNotice[r].id)
            data[i].isRead = true
        }
      }
      that.setData({
        noticeData: data
      })
      wx.setStorageSync("readNotice", data);
      // 设置滚动
      if (!that.data.si) {
        var si = setInterval(function() {
            var index = parseInt(that.data.toView.replace(/to/, ""))
            if (index + 1 >= that.data.noticeData.length)
              index = -1
            that.setData({
              toView: 'to' + (++index)
            })
          },
          3000)
        that.data.si = 1
      }
    })


  },
  tomanager: function() {
    wx.navigateTo({
      url: '/pages/gangManage/productManage/productManage',
    })

  },
  tobanghui: function() {
    var that = this;
    var gangId = that.data.userInfo.gangId;
    if (gangId == null) {
      var user = wx.getStorageSync('userInfo');
      gangId = user.gangId;
    }
    wx.navigateTo({
      url: '/pages/banghui/details?id=' + gangId,
    })
  },
  tohongbao: function() {
    wx.navigateTo({
      url: '/pages/user/hongbao',
    })
  },
  tomyshop: function() {
    wx.navigateTo({
      url: '/pages/user/myshop',
    })
  },
  menuTap: function(e) {
    var selectMenu = this.data.selectMenu;
    selectMenu.isShow = !selectMenu.isShow;
    if (selectMenu.current != e.currentTarget.dataset.index)
      selectMenu.isShow = true;
    selectMenu.current = e.currentTarget.dataset.index;
    this.setData({
      selectMenu: selectMenu
    })
  },

  childMenuTap: function(e) {
    var that = this;
    var selectMenu = this.data.selectMenu;
    selectMenu.selectedCurrent = e.currentTarget.dataset.pid;
    selectMenu.selectedChild[selectMenu.selectedCurrent] = e.currentTarget.dataset.index;
    selectMenu.selectedChildName[selectMenu.selectedCurrent] = e.currentTarget.dataset.name;
    selectMenu.isShow = false;
    this.setData({
      selectMenu: selectMenu
    })
    var typeid = selectMenu.selectedCurrent == 0 ? selectMenu.selectedChild[0] : "";
    var brandid = selectMenu.selectedCurrent == 1 ? selectMenu.selectedChild[1] : "";
    //商品列表
    http.post({
      url: "product/getProductList",
      type: typeid,
      brand: brandid
    }, function(data) {
      console.log('商品列表', data)
      that.setData({
        productList: data
      })
    })
  }
})